package com.carleasoft.mps.adu.business.position.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.adu.business.position.bean.po.AuthorityPositionGroupPagePo;
import com.carleasoft.mps.adu.business.position.bean.po.AuthorityPositionGroupPo;
import com.carleasoft.mps.adu.business.position.bean.vo.AuthorityPositionGroupVo;
import com.carleasoft.mps.adu.business.position.bean.vo.PositionGroupVo;
import com.carleasoft.mps.adu.business.position.service.AuthorityPositionGroupService;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.user.SortPo;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>Description: positionGroup controller</p>
 *
 * @author ztm
 * @since 2020-05-12
 */
@RestController
@RequestMapping("/positionGroup")
public class AuthorityPositionGroupController {

    @Autowired
    AuthorityPositionGroupService authorityPositionGroupService;


    /**
     * 添加岗位分组管理
     *
     * @param authorityPositionGroupPo
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody AuthorityPositionGroupPo authorityPositionGroupPo) {
        IBeanUtil.paramVerify(authorityPositionGroupPo, "positionGroupName");
        if(StrUtil.isBlank(authorityPositionGroupPo.getParentPositionGroupCode())) {
            if (StrUtil.isBlank(authorityPositionGroupPo.getDeptCode())) {
                //默认使用顶级部门，即组织code
                authorityPositionGroupPo.setDeptCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
            }
            authorityPositionGroupPo.setParentPositionGroupCode(authorityPositionGroupPo.getDeptCode());
        }
        return R.ok(authorityPositionGroupService.add(authorityPositionGroupPo));
    }

    /**
     * 幂等检测职位分组名称
     * @param authorityPositionGroupPo
     * @return
     */
    @PostMapping("/checkPositionGroup")
    public R checkPositionGroup(@RequestBody AuthorityPositionGroupPo authorityPositionGroupPo){
        IBeanUtil.paramVerify(authorityPositionGroupPo, "positionGroupName");
        Boolean isOk=authorityPositionGroupService.checkPositionGroup(authorityPositionGroupPo);
        return R.ok(isOk);
    }



    /**
     * 修改岗位分组管理
     *
     * @param authorityPositionGroupPo
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody AuthorityPositionGroupPo authorityPositionGroupPo) {
        IBeanUtil.paramVerify(authorityPositionGroupPo, "positionGroupCode");
        if(StrUtil.isBlank(authorityPositionGroupPo.getParentPositionGroupCode())) {
            if (StrUtil.isBlank(authorityPositionGroupPo.getDeptCode())) {
                //默认使用顶级部门，即组织code
                authorityPositionGroupPo.setDeptCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
            }
            authorityPositionGroupPo.setParentPositionGroupCode(authorityPositionGroupPo.getDeptCode());
        }
        return R.ok(authorityPositionGroupService.update(authorityPositionGroupPo));
    }

    /**
     * 分页查询岗位分组管理
     *
     * @param authorityPositionGroupPagePo
     * @return
     */
    @PostMapping("/list")
    public R select(@RequestBody AuthorityPositionGroupPagePo authorityPositionGroupPagePo) {
        if(StrUtil.isBlank(authorityPositionGroupPagePo.getParentPositionGroupCode())){
            if(StrUtil.isNotBlank(authorityPositionGroupPagePo.getDeptCode())){
                authorityPositionGroupPagePo.setParentPositionGroupCode(authorityPositionGroupPagePo.getDeptCode());
            }else {
                authorityPositionGroupPagePo.setParentPositionGroupCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
            }
        }
        return R.ok( authorityPositionGroupService.doFindList(new Page(authorityPositionGroupPagePo.getCurrent(),authorityPositionGroupPagePo.getSize()), authorityPositionGroupPagePo));
    }

    /**
     * 查询岗位分组管理树
     *
     * @return
     */
    @PostMapping("/tree")
    public R selectTree(AuthorityPositionGroupPo authorityPositionGroupPo) {
        if(StrUtil.isBlank(authorityPositionGroupPo.getParentPositionGroupCode())){
            if(StrUtil.isNotBlank(authorityPositionGroupPo.getDeptCode())){
                authorityPositionGroupPo.setParentPositionGroupCode(authorityPositionGroupPo.getDeptCode());
            }else {
                authorityPositionGroupPo.setParentPositionGroupCode(ThreadUserContext.getCurrentCacheUser().getOrgCodeJoin());
            }
        }
        List<PositionGroupVo> treeGroup = authorityPositionGroupService.doFindTree(authorityPositionGroupPo);
        return R.ok(treeGroup);
    }

    /**
     * 根据id查询岗位分组管理详情
     *
     * @param authorityPositionGroupPagePo
     * @return
     */
    @PostMapping("/selectByCode")
    public R selectByCode(@RequestBody AuthorityPositionGroupPagePo authorityPositionGroupPagePo) {
        return R.ok(authorityPositionGroupService.doFindByCode(authorityPositionGroupPagePo.getPositionGroupCode()));
    }

    /**
     * 排序
     * @param sortPo
     * @return
     */
    @PostMapping("/updateSort")
    public R updateSort(@RequestBody SortPo sortPo){
        IBeanUtil.paramVerify(sortPo, "code");
        return R.result(authorityPositionGroupService.updateSort(sortPo));
    }


    /**
     * 根据id删除岗位分组管理
     *
     * @param authorityPositionGroupPagePo
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody AuthorityPositionGroupPagePo authorityPositionGroupPagePo) {
        return R.ok(authorityPositionGroupService.deleteByCode(authorityPositionGroupPagePo.getPositionGroupCode()));
    }
}
